package qldc.pm.tiensu.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import qldc.pm.qtht.dmdc.utils.StringUtils;
import qldc.pm.tiensu.NoSuchBienBanBanGiaoHoSoException;
import qldc.pm.tiensu.dto.CongViecBanGiaoDTO;
import qldc.pm.tiensu.model.BienBanBanGiaoHoSo;
import qldc.pm.tiensu.service.CongViecBanGiaoLocalServiceUtil;
import qldc.pm.tiensu.service.base.BienBanBanGiaoHoSoLocalServiceBaseImpl;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.Validator;

/**
 * The implementation of the bien ban ban giao ho so local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link qldc.pm.tiensu.service.BienBanBanGiaoHoSoLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see qldc.pm.tiensu.service.base.BienBanBanGiaoHoSoLocalServiceBaseImpl
 * @see qldc.pm.tiensu.service.BienBanBanGiaoHoSoLocalServiceUtil
 */
public class BienBanBanGiaoHoSoLocalServiceImpl extends BienBanBanGiaoHoSoLocalServiceBaseImpl {
	private static Log _log = LogFactoryUtil.getLog(BienBanBanGiaoHoSoLocalServiceImpl.class);

	public BienBanBanGiaoHoSo findBienBanBanGiaoHoSoId(long bienBanBanGiaoHoSoId) {
		BienBanBanGiaoHoSo bienBanBanGiaoHoSo = null;

    	try {
    		bienBanBanGiaoHoSo = bienBanBanGiaoHoSoPersistence.findByPrimaryKey(bienBanBanGiaoHoSoId);
		} catch (SystemException e) {
			_log.warn(e.getMessage());
		} catch (NoSuchBienBanBanGiaoHoSoException e) {
			_log.warn(e.getMessage());
		}

    	return bienBanBanGiaoHoSo;
	}

	public BienBanBanGiaoHoSo addBienBan( String kinhGui,
			String nguoiLap, String nguoiDuocBanGiao,
			String chucVu, String chucVuNguoiDuocBanGiao,
			String noiDung, Date ngayLap, Date ngayBanGiao,String diaDiemBanGiao, List<CongViecBanGiaoDTO> congViecs)
					throws SystemException {
		long bienBanBanGiaoHoSoId = counterLocalService.increment(BienBanBanGiaoHoSo.class.getName());
		BienBanBanGiaoHoSo bienBanBanGiaoHoSo = createBienBanBanGiaoHoSo(bienBanBanGiaoHoSoId);

		bienBanBanGiaoHoSo.setKinhGui(kinhGui);
		bienBanBanGiaoHoSo.setNguoiLap(nguoiLap);
		bienBanBanGiaoHoSo.setNguoiDuocBanGiao(nguoiDuocBanGiao);
		bienBanBanGiaoHoSo.setChucVu(chucVu);
		bienBanBanGiaoHoSo.setChucVuNguoiDuocBanGiao(chucVuNguoiDuocBanGiao);
		bienBanBanGiaoHoSo.setNoiDung(noiDung);
		bienBanBanGiaoHoSo.setNgayLap(ngayLap);
		bienBanBanGiaoHoSo.setNgayBanGiao(ngayBanGiao);
		bienBanBanGiaoHoSo.setDiaDiemBanGiao(diaDiemBanGiao);
		//add new
		BienBanBanGiaoHoSo bb = addBienBanBanGiaoHoSo(bienBanBanGiaoHoSo);
		for (CongViecBanGiaoDTO cv :congViecs){
			CongViecBanGiaoLocalServiceUtil.addCongViec(bb.getBienBanBanGiaoHoSoId(), cv.getTenCongViec(), cv.getTienDoThucHien(), cv.getChuThich());
		}

		return bb;
	}

	public BienBanBanGiaoHoSo updateBienBan(
			long bienBanBanGiaoId, String kinhGui,
		String nguoiLap, String nguoiDuocBanGiao,
		String chucVu, String chucVuNguoiDuocBanGiao,
		String noiDung, Date ngayLap, Date ngayBanGiao, String diaDiemBanGiao,List<CongViecBanGiaoDTO> congViecs) throws SystemException {
		BienBanBanGiaoHoSo bienBanBanGiaoHoSo = findBienBanBanGiaoHoSoId(bienBanBanGiaoId);

		if (Validator.isNotNull(bienBanBanGiaoHoSo)) {
			bienBanBanGiaoHoSo.setKinhGui(kinhGui);
			bienBanBanGiaoHoSo.setNguoiLap(nguoiLap);
			bienBanBanGiaoHoSo.setNguoiDuocBanGiao(nguoiDuocBanGiao);
			bienBanBanGiaoHoSo.setChucVu(chucVu);
			bienBanBanGiaoHoSo.setChucVuNguoiDuocBanGiao(chucVuNguoiDuocBanGiao);
			bienBanBanGiaoHoSo.setNoiDung(noiDung);
			bienBanBanGiaoHoSo.setNgayLap(ngayLap);
			bienBanBanGiaoHoSo.setNgayBanGiao(ngayBanGiao);
			bienBanBanGiaoHoSo.setDiaDiemBanGiao(diaDiemBanGiao);

			congViecBanGiaoPersistence.removeByBienBanBanGiaoHoSoId(bienBanBanGiaoId);

			for (CongViecBanGiaoDTO cv :congViecs){
				CongViecBanGiaoLocalServiceUtil.addCongViec(bienBanBanGiaoId, cv.getTenCongViec(), cv.getTienDoThucHien(), cv.getChuThich());
			}
		}

		return updateBienBanBanGiaoHoSo(bienBanBanGiaoHoSo);
	}

	public List<BienBanBanGiaoHoSo> findByNguoiDuocBanGiao_ChucVu(String nguoiDuocBanGiao,String chucVuNguoiDuocBanGiao, int start, int end,OrderByComparator order) {
		List<BienBanBanGiaoHoSo> result = new ArrayList<BienBanBanGiaoHoSo>();
		try {
			if (Validator.isNull(nguoiDuocBanGiao)) {
				nguoiDuocBanGiao = "";
			}

			if (Validator.isNull(chucVuNguoiDuocBanGiao)) {
				chucVuNguoiDuocBanGiao = "";
			}

			nguoiDuocBanGiao = StringUtils.getLike(nguoiDuocBanGiao);
			chucVuNguoiDuocBanGiao = StringUtils.getLike(chucVuNguoiDuocBanGiao);
			result = bienBanBanGiaoHoSoPersistence.findByNguoiDuocBanGiao_ChucVu(nguoiDuocBanGiao, chucVuNguoiDuocBanGiao, start, end, order);
		} catch (SystemException e) {
		}

		return result;
	}

	public int countByNguoiDuocBanGiao_ChucVu(String nguoiDuocBanGiao,String chucVuNguoiDuocBanGiao) {
		int count = 0;

		try {
			if (Validator.isNull(nguoiDuocBanGiao)) {
				nguoiDuocBanGiao = "";
			}

			if (Validator.isNull(chucVuNguoiDuocBanGiao)) {
				chucVuNguoiDuocBanGiao = "";
			}

			nguoiDuocBanGiao = StringUtils.getLike(nguoiDuocBanGiao);
			chucVuNguoiDuocBanGiao = StringUtils.getLike(chucVuNguoiDuocBanGiao);
			count = bienBanBanGiaoHoSoPersistence.countByNguoiDuocBanGiao_ChucVu(nguoiDuocBanGiao, chucVuNguoiDuocBanGiao);
		} catch (SystemException e) {
		}

		return count;
	}
}
